package com.ruoyi.record.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.basic.domain.BasicPost;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.service.IBasicPostService;
import com.ruoyi.basic.service.IBasicUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.record.domain.RecordCompete;
import com.ruoyi.record.service.IRecordCompeteService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 职称申报Controller
 * 
 * @author ruoyi
 * @date 2024-05-09
 */
@Controller
@RequestMapping("/record/compete")
public class RecordCompeteController extends BaseController
{
    private String prefix = "record/compete";

    @Autowired
    private IRecordCompeteService recordCompeteService;

    @Autowired
    private IBasicUserService basicUserService;

    @Autowired
    private IBasicPostService basicPostService;

    @RequiresPermissions("record:compete:view")
    @GetMapping()
    public String compete(ModelMap mmap)
    {
        mmap.put("isAdmin", ShiroUtils.getSysUser().isAdmin());
        return prefix + "/compete";
    }

    /**
     * 查询职称申报列表
     */
    @RequiresPermissions("record:compete:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecordCompete recordCompete)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin()) {
            recordCompete.setCreateBy(ShiroUtils.getLoginName());
        }
        List<RecordCompete> list = recordCompeteService.selectRecordCompeteList(recordCompete);
        return getDataTable(list);
    }

    /**
     * 新增职称申报
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        BasicUser basicUser = basicUserService.selectBasicUserByIdCard(ShiroUtils.getLoginName());
        BasicPost basicPost = new BasicPost();
        if(StringUtils.isNotNull(basicUser)) {
            basicPost = basicPostService.getOne(new LambdaQueryWrapper<BasicPost>().eq(BasicPost::getUserId, basicUser.getId()).eq(BasicPost::getIsDefault, "Y").last("limit 1"));
        }
        mmap.put("basicPost", basicPost);
        return prefix + "/add";
    }

    /**
     * 新增保存职称申报
     */
    @RequiresPermissions("record:compete:add")
    @Log(title = "职称竞聘", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecordCompete recordCompete)
    {
        recordCompete.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recordCompeteService.insertRecordCompete(recordCompete));
    }

    /**
     * 修改职称申报
     */
    @GetMapping("/edit/{competeId}")
    public String edit(@PathVariable("competeId") Long competeId, ModelMap mmap)
    {
        RecordCompete recordCompete = recordCompeteService.selectRecordCompeteByCompeteId(competeId);
        mmap.put("recordCompete", recordCompete);
        return prefix + "/edit";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("record:compete:edit")
    @Log(title = "职称竞聘", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecordCompete recordCompete)
    {
        recordCompete.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recordCompeteService.updateRecordCompete(recordCompete));
    }

    /**
     * 删除职称申报
     */
    @RequiresPermissions("record:compete:remove")
    @Log(title = "职称竞聘", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recordCompeteService.deleteRecordCompeteByCompeteIds(ids));
    }

    /**
     * 职称申报提交
     */
    @RequiresPermissions("record:compete:submit")
    @Log(title = "职称竞聘提交", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    @ResponseBody
    public AjaxResult submit(Long competeId)
    {
        recordCompeteService.update(null, new LambdaUpdateWrapper<RecordCompete>().set(RecordCompete::getSubmitFlag, "1")
                .set(RecordCompete::getSubmitTime, DateUtils.getNowDate()).eq(RecordCompete::getCompeteId, competeId));
        return success();
    }

    @RequiresPermissions("verify:compete:view")
    @GetMapping("/verify")
    public String verify()
    {
        return prefix + "/verify/compete";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("verify:compete:list")
    @PostMapping("/verify/list")
    @ResponseBody
    public TableDataInfo verifyList(RecordCompete recordCompete)
    {
        startPage();
        List<RecordCompete> list = recordCompeteService.selectVerifyCompeteList(recordCompete);
        return getDataTable(list);
    }

    @Log(title = "职称竞聘", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecordCompete> util = new ExcelUtil<RecordCompete>(RecordCompete.class);
        List<RecordCompete> competeList = BeanUtils.copyList(util.importExcel(file.getInputStream()), RecordCompete::new);
        String message = recordCompeteService.importRecordCompete(competeList, updateSupport);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecordCompete> util = new ExcelUtil<RecordCompete>(RecordCompete.class);
        return util.importTemplateExcel("职称申报");
    }

    /**
     * 导出职称申报列表
     */
    @Log(title = "职称竞聘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecordCompete recordCompete)
    {
        List<RecordCompete> list = recordCompeteService.selectVerifyCompeteList(recordCompete);
        ExcelUtil<RecordCompete> util = new ExcelUtil<RecordCompete>(RecordCompete.class);
        return util.exportExcel(list, "职称竞聘");
    }

    /**
     * 职称申报提交
     */
    @RequiresPermissions("verify:compete:cancel")
    @Log(title = "职称竞聘撤销", businessType = BusinessType.OTHER)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(Long competeId)
    {
        recordCompeteService.update(null, new LambdaUpdateWrapper<RecordCompete>().set(RecordCompete::getSubmitFlag, "0")
                .set(RecordCompete::getSubmitTime, null).eq(RecordCompete::getCompeteId, competeId));
        return success();
    }

    /**
     * 修改职称申报
     */
    @GetMapping("/verify/{competeId}")
    public String verify(@PathVariable("competeId") Long competeId, ModelMap mmap)
    {
        RecordCompete recordCompete = recordCompeteService.selectRecordCompeteByCompeteId(competeId);
        mmap.put("recordCompete", recordCompete);
        return prefix + "/verify/verify";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("verify:compete:save")
    @Log(title = "职称竞聘审批", businessType = BusinessType.UPDATE)
    @PostMapping("/verify/save")
    @ResponseBody
    public AjaxResult verifySave(RecordCompete recordCompete)
    {
        return toAjax(recordCompeteService.verifyRecordCompete(recordCompete));
    }

    /**
     * 职称申报得分
     * @param userType
     * @param mmap
     * @return
     */
    @RequiresPermissions("score:compete:view")
    @GetMapping("/score/{userType}")
    public String score(@PathVariable("userType") String userType, ModelMap mmap)
    {
        mmap.put("userType", userType);
        return prefix + "/score/compete";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("score:compete:list")
    @PostMapping("/score/list")
    @ResponseBody
    public TableDataInfo scoreList(RecordCompete recordCompete)
    {
        startPage();
        List<RecordCompete> list = recordCompeteService.selectScoreCompeteList(recordCompete);
        return getDataTable(list);
    }

    /**
     * 职称申报得分
     */
    @GetMapping("/score/{competeId}/{userType}")
    public String score(@PathVariable("competeId") Long competeId, @PathVariable("userType") String userType, ModelMap mmap)
    {
        RecordCompete recordCompete = recordCompeteService.selectRecordCompeteByCompeteId(competeId);
        mmap.put("recordCompete", recordCompete);
        mmap.put("userType", userType);
        return prefix + "/score/score";
    }

    /**
     * 修改保存职称申报
     */
    @RequiresPermissions("score:compete:save")
    @Log(title = "职称竞聘审批", businessType = BusinessType.UPDATE)
    @PostMapping("/score/save")
    @ResponseBody
    public AjaxResult saveSave(RecordCompete recordCompete)
    {
        return toAjax(recordCompeteService.scoreRecordCompete(recordCompete));
    }
}
